package aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * XML文件修改：
 * 对于xml文件
 * 1.需要添加xmlns、schemaLocation对应的aop说明
 * 2.需要添加<aop:aspectj-autoproxy/>
 * 3.定以切点、切面类、通知
 * 
 * 
 * 在面向切面编程过程中，类一般分为两种： 1. 业务类  2. 切面类
 * 对于切面类
 * 
 * 对于execution表达式，不能使用 *..*.*(..) 而是直接用*(..)就行，否则会报错
 * 
 * @author lcx
 */

@Component("aspectClass")
@Aspect
public class AspectClass {
	@Pointcut("execution(* *..*.*Before(..))")  
	private void anyBeforeMethod(){}//定义一个切入点  
	
	@Pointcut("execution(* *..*.*After(..))")  
	private void anyAfterMethod(){}//定义一个切入点  
	
	@Pointcut(value="execution(* *..*.*AfterPara(String)) && args(businessmsg)") 
	private void anyAfterParaMethod(String businessmsg){}//定义一个切入点  
	
		@Pointcut("execution(* *..*.*Around(..))")  
	private void anyAroundMethod(){}//定义一个切入点  

	
	@Before("anyBeforeMethod()")
	public void before()
	{
		System.out.println("... AspectClass do something before");
	}
	
	@After("anyAfterParaMethod(businessmsg)")
	public void afterPara(String businessmsg)
	{
		System.out.println("... AspectClass do something after");
		System.out.println("businessmsg=          "+businessmsg);
	}	

	@After("anyAfterMethod()")
	public void after()
	{
		System.out.println("... AspectClass do something after");
	}	

	@AfterReturning("anyBeforeMethod()")
	public void afterReturning()
	{
		System.out.println("... AspectClass do something afterReturning");
	}


	public void afterThrowing()
	{
		System.out.println("... AspectClass do something afterThrowing");
	}

	@Around("anyAroundMethod()")
	public int around(ProceedingJoinPoint point)
	{
		before();
		long t1=System.currentTimeMillis();
		try {
			point.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		long t2=System.currentTimeMillis();
		after();
		System.out.println("AspectClass calculate time spent    "+(t2-t1)+"   ms");
		return (int) (t2-t1);
	}

}
